package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/submit")
    public Result add(@RequestBody Map map){
        //验证手机验证码
        String telphone =(String) map.get("telephone");
        String validateCode =(String) map.get("validateCode");

        //验证验证码
        String code= jedisPool.getResource().get(telphone + RedisMessageConstant.SENDTYPE_ORDER);

        if(code == null || !code.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        //添加订单

        Result result = null;
        try {
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            System.out.println(map);
            result = orderService.add(map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //发送短信
       /* if(result.isFlag()){
            String orderDate = (String) map.get("orderDate");
            try {
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telphone,code.toString());
            } catch (ClientException e) {
                e.printStackTrace();
            }

        }
       */
        return result;

    }

  /*  @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Order order = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }*/
}
